<?php
namespace app\oa\ext;
use app\oa\ext\UserStatus;
use app\oa\model\File;
use app\oa\model\Task;
use think\Controller;
use think\Request;

class Core extends Controller{
    /**
     * @param Request $request
     * @param $tid // task id
     */
    public function upload(Request $request, $tid) {
        if(!UserStatus::isLogin()) {
            $this->error("没有登录");
        }

        $time = time();

        $file = $request->file('file');
        if(empty($file)){
            $this->error("请选择上传文件");
        }
        $info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'oa', $time . $file->getExtension(), false);
        if(!$info) {
            $this->error("上传文件失败");
        }

        $file = new File();
        $file['task_id'] = $tid;
        $file['sender_id'] = UserStatus::getLoginID();
        $file['path'] = '/static/oa/' . $info->getFilename();

        if($file->save()) {
            $this->success("上传成功");
        }else {
            $this->error("上传失败");
        }
    }

    /**
     * 检查task是否是指定状态
     * @param $id   // task id
     * @param $status   // task 的 status
     */
    public function checkStatus($id, $status) {
        if(Task::get($id)['status'] != $status) {
            $this->error("权限错误");
        }
    }
    public function checkDepartment($id) {
        if(Task::get($id)['department_id'] != UserStatus::getDepartmentID()) {
            $this->error("权限错误");
        }
    }


}